﻿//------------------------------------------------------------------------------
// <copyright file="Sample.cs" company="Robert MacLean">
//      Copyright © Robert MacLean.  All Rights Reserved.
// </copyright>
//
//  This code released under the terms of the 
//  Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)
//------------------------------------------------------------------------------

namespace FindResultsTweak
{
    using System;

    /// <summary>
    /// 
    /// </summary>
    class Sample
    {
        /// <summary>
        /// Gets or sets the path.
        /// </summary>
        /// <value>The path.</value>
        public string Path { get; set; }

        /// <summary>
        /// Gets or sets the filename.
        /// </summary>
        /// <value>The filename.</value>
        public string Filename { get; set; }

        /// <summary>
        /// Gets or sets the drive.
        /// </summary>
        /// <value>The drive.</value>
        public string Drive { get; set; }

        /// <summary>
        /// Gets or sets the directory.
        /// </summary>
        /// <value>The directory.</value>
        /// 
        public string Directory { get; set; }
        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the extension.
        /// </summary>
        /// <value>The extension.</value>
        public string Extension { get; set; }

        /// <summary>
        /// Gets or sets the line.
        /// </summary>
        /// <value>The line.</value>
        public string Line { get; set; }

        /// <summary>
        /// Gets or sets the end col if first.
        /// </summary>
        /// <value>The end col if first.</value>
        public string EndColIfFirst { get; set; }

        /// <summary>
        /// Gets or sets the span end line.
        /// </summary>
        /// <value>The span end line.</value>
        public string SpanEndLine { get; set; }

        /// <summary>
        /// Gets or sets the span end column.
        /// </summary>
        /// <value>The span end column.</value>
        public string SpanEndColumn { get; set; }

        /// <summary>
        /// Gets or sets the matched text.
        /// </summary>
        /// <value>The matched text.</value>
        public string MatchedText { get; set; }

        /// <summary>
        /// Gets or sets the text of first line.
        /// </summary>
        /// <value>The text of first line.</value>
        public string TextOfFirstLine { get; set; }

        /// <summary>
        /// Gets or sets the summary of hit.
        /// </summary>
        /// <value>The summary of hit.</value>
        public string SummaryOfHit { get; set; }

        /// <summary>
        /// Gets or sets the text of spanned lines.
        /// </summary>
        /// <value>The text of spanned lines.</value>
        public string TextOfSpannedLines { get; set; }

        /// <summary>
        /// Gets or sets the column.
        /// </summary>
        /// <value>The column.</value>
        public string Column { get; set; }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <param name="format">The format.</param>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public string ToString(string format)
        {
            string result = string.Empty;

            for (int counter = 0; counter < format.Length; counter++)
            {
                bool NotHandled = false;
                switch (format[counter])
                {
                    case '\\':
                        {
                            if (counter + 1 < format.Length)
                            {
                                switch (format[counter + 1])
                                {
                                    case '\\':
                                        {
                                            counter++;
                                            result += '\\';
                                            break;
                                        }
                                    case 'n':
                                        {
                                            counter++;
                                            result += Environment.NewLine;
                                            break;
                                        }
                                    case '$':
                                        {
                                            counter++;
                                            result += '$';
                                            break;
                                        }
                                    case 's':
                                        {
                                            counter++;
                                            result += ' ';
                                            break;
                                        }
                                    case 't':
                                        {
                                            counter++;
                                            result += "\t";
                                            break;
                                        }
                                    default:
                                        {
                                            NotHandled = true;
                                            break;
                                        }
                                }
                            }
                            else
                            {
                                NotHandled = true;                                
                            }

                            break;
                        }
                    case '$':
                        {
                            if (counter + 1 < format.Length)
                            {
                                switch (format[counter + 1])
                                {
                                    case 'p':
                                        {
                                            counter++;
                                            result += this.Path;
                                            break;
                                        }
                                    case 'f':
                                        {
                                            counter++;
                                            result += this.Filename;
                                            break;
                                        }
                                    case 'v':
                                        {
                                            counter++;
                                            result += this.Drive;
                                            break;
                                        }
                                    case 'n':
                                        {
                                            counter++;
                                            result += this.Name;
                                            break;
                                        }
                                    case 'e':
                                        {
                                            counter++;
                                            result += this.Extension;
                                            break;
                                        }
                                    case 'l':
                                        {
                                            counter++;
                                            result += this.Line;
                                            break;
                                        }
                                    case 'c':
                                        {
                                            counter++;
                                            result += this.Column;
                                            break;
                                        }
                                    case 'x':
                                        {
                                            counter++;
                                            result += this.EndColIfFirst;
                                            break;
                                        }
                                    case 'L':
                                        {
                                            counter++;
                                            result += this.SpanEndLine;
                                            break;
                                        }
                                    case 'C':
                                        {
                                            counter++;
                                            result += this.SpanEndColumn;
                                            break;
                                        }
                                    case '0':
                                        {
                                            counter++;
                                            result += this.MatchedText;
                                            break;
                                        }
                                    case 't':
                                        {
                                            counter++;
                                            result += this.TextOfFirstLine;
                                            break;
                                        }
                                    case 's':
                                        {
                                            counter++;
                                            result += this.SummaryOfHit;
                                            break;
                                        }
                                    case 'T':
                                        {
                                            counter++;
                                            result += this.TextOfSpannedLines;
                                            break;
                                        }
                                    default:
                                        {
                                            NotHandled = true;
                                            break;
                                        }
                                }
                            }
                            else
                            {
                                NotHandled = true;                                
                            }

                            break;
                        }
                    default:
                        {
                            NotHandled = true;
                            break;
                        }
                }

                if (NotHandled)
                {
                    result += format[counter];
                }
            }

            return result;
        }
    }
}
